<?php
function echoSelect($name, $count, $selected = "", $extras = "", $tds = true) {
    if ($tds) {
        echo "<td>";
    }
    echo "<select name=\"" . $name . "\" $extras>\n";
    for ($i = 0; $i < $count; $i++) {
        echo "<option value=\"" . $i . "\"";
        if ($i == $selected) {
            echo " selected ";
        }
        echo ">" . $i . "</option>\n";
    }
    echo "</select>\n";
    if ($tds) {
        echo "</td>\n";
    }
}

function echoSelectArray($name, $array, $selected = "", $script = "", $class = "") {
    echo "<select name=\"" . $name . "\" $script $class>\n";
    foreach (array_keys($array) as $key) {
        $value = $array[$key];
        echo "<option value=\"" . $key . "\"";
        if ($value == $selected) {
            echo " selected ";
        }
        echo ">" . $value . "</option>\n";
    }
    echo "</select></td>\n";
}

function getEchoTR($rowcount, $align = "left") {
    if ($rowcount % 2 == 1) {
        return "<tr class=\"row1\" align=\"" . $align . "\">";
    } else {
        return "<tr class=\"row2\" align=\"" . $align . "\">";
    }
}

function echoTR($rowcount, $align = "left") {
    echo getEchoTR($rowcount, $align);
}

function getEchoValue($value) {
    if (isset ($value) and strlen($value) > 0) {
        return $value;
    } else {
        return "&nbsp;";
    }
}

function getEchoValueShrink($value, $shrinkLen) {
    $strlen = strlen($value);
    if (isset ($value) and $strlen > 0) {
        if ($strlen > (2 * $shrinkLen)) {
            return "<font size=\"-2\">" . $value . "</font>";
        } else
            if ($strlen > $shrinkLen) {
                return "<font size=\"-1\">" . $value . "</font>";
            } else {
                return $value;
            }
    } else {
        return "&nbsp;";
    }
}

function echoRaceLinks($races) {
    $lastRaceName = "";
    foreach ($races as $race) {
        $name = $race->name;
        $shortName = $race->getShortRuleSetName();
        if (0 == strcmp($lastRaceName, $race->name)) {
            // Different rule set for same race
            echo<<< EOQ

    	    &nbsp;<a href="race_viewer.php?race_id=$race->id"><span class="tiny">$shortName</span></a>
EOQ;
        } else {
            echo<<< EOQ

        <br />&nbsp;<a class="sub" href="race_viewer.php?race_id=$race->id">$race->name <span class="tiny">$shortName</span></a>
EOQ;
        }
        $lastRaceName = $race->name;
    }
}

function echoRaceSelect($races, $currentRace) {
    echo "<select name=\"race_id\">";
    foreach ($races as $race) {
        echo "<option value=\"" . $race->id . "\"";
        if ($currentRace->id == $race->id) {
            echo " selected";
        }
        echo ">". $race->getRaceDisplay(). "</option>\n";
    }
    echo "</select>\n";
}

function getProb($rollRequired) {
    $value = 0;
    switch ($rollRequired) {
        case 2 :
            $value += 1;
        case 3 :
            $value += 2;
        case 4 :
            $value += 3;
        case 5 :
            $value += 4;
        case 6 :
            $value += 5;
        case 7 :
            $value += 6;
        case 8 :
            $value += 5;
        case 9 :
            $value += 4;
        case 10 :
            $value += 3;
        case 11 :
            $value += 2;
        case 12 :
            $value += 1;
    }
    return $value;
}

function executeQuery($query) {
    global $MAILTO, $leagueNameShort;
    $rs = mysql_query($query) or die($query . "<br /><br />\n" . mysql_error() . "<br /><br />\nPlease email this message to <a href=\"mailto:" . $MAILTO . "?body=" . $leagueNameShort . " Error, msg=" . mysql_error() . ", query=$query\">Webmaster (" . $MAILTO . ")</a><br />");
    if (!ereg("^select", $query)) {
        mysql_query("update updates set update_timestamp=now()");
        logger($query);
    }
    return $rs;
}

function getZeroForNull($value) {
    if (isset ($value) and $value) {
        return $value;
    }
    return 0;
}

function logger($message) {
    global $loggedin;
    if ($file = fopen("logfile.log", "a")) {
        fputs($file, date("\nF j, Y, g:i a\t"));
        $who = "<Unknown>\t";
        if (isset ($loggedin)) {
            $who = "<" . $loggedin->name . ">\t";
        }
        fputs($file, $who);
        fputs($file, $message);
        fclose($file);
    }
}

/**
 * Function to check and see if the currently logged in user is a league commissioner
 */
function isCommissioner() {
    global $loggedin;
    return isset ($loggedin) and $loggedin->isAdmin();
}

/**
 * Function to get a string representation of yes/no flags
 */
function getYesNo($value) {
    if (isset ($value) and $value) {
        return "Yes";
    } else {
        return "No";
    }
}

/**
 * Function to get a string representing fan factor change
 */
function getFanText($fanChange) {
    if (!isset ($fanChange) || $fanChange == 0) {
        return "Unchanged";
    } else
        if ($fanChange == 1) {
            return "Up";
        } else
            if ($fanChange == -1) {
                return "Down";
            } else {
                return $fanChange;
            }
}

/**
 * This function returns the elements from the source that do not have matching keys in the compare 
 */
function array_key_diff(& $source, $compare) {
    $destination = array ();
    foreach ($source as $skill) {
        if (!isset ($compare[$skill->id])) {
            $destination[$skill->id] = $skill;
        }
    }
    return $destination;
}

function getLatestMatches($number) {
    $rows = array ();
    $rs = executeQuery("select match_id, date_format(date_played, '%b %e'), home_record, away_record from matches order by date_played desc, match_id desc LIMIT 0, $number");
    while ($row = mysql_fetch_row($rs)) {
        array_push($rows, $row);
    }
    return $rows;
}

function echoNewsMatch(& $teams, & $races) {
    $rows = getLatestMatches(5);
    $mrd = new MatchRecordDao();
    foreach ($rows as $row) {
        $homeRecord = $mrd->load($row[2]);
        $awayRecord = $mrd->load($row[3]);
        $homeTeam = $teams->getTeam($homeRecord->teamId);
        $awayTeam = $teams->getTeam($awayRecord->teamId);

        $diff = $homeRecord->score - $awayRecord->score;
        $matchId = $row[0];
        $date = $row[1];
        $msg = " beat ";
        if ($diff < -2) {
        	$msg = " were thrashed by ";
        } else
            if ($diff < 0) {
                $msg = " lost to ";
            } else
                if ($diff == 0) {
                    $msg = " drew with ";
                } else
                    if ($diff > 2) {
                        $msg = " hammered ";
                    }
		
        echo<<< EOQ
    <a href="match_view.php?matchId=$matchId">$date $homeTeam->name $msg $awayTeam->name $homeRecord->score-$awayRecord->score</a><br />
EOQ;
    }
}

function getVariable($name, $index, $getVars) {
    $string = $name . $index;
    return $getVars[$string];
}

function isNotEmpty($string) {
    return isset($string) && strlen($string) > 0;
}

function isNotBlank($string) {
    return isset($string) && strlen(trim($string)) > 0;
}

function getParam($name) {
    global $_GET, $_POST;
    if (isset($_GET[$name])) {
    	 return $_GET[$name];
    }
    if (isset($_POST[$name])) {
    	 return $_POST[$name];
    }
    return null;
}

function getSession($name) {
    global $_SESSION;
    if (isset($_SESSION[$name])) {
    	 return $_SESSION[$name];
    }
    return null;
}
?>